<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单号" prop="orderNumber">
        <el-input
          v-model="queryParams.orderNumber"
          placeholder="请输入订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品标题" prop="productTitle">
        <el-input
          v-model="queryParams.productTitle"
          placeholder="请输入商品标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="支付时间" prop="payTime">
        <el-date-picker clearable
          v-model="queryParams.payTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择支付时间">
        </el-date-picker>
      </el-form-item>

<!--      <el-form-item label="发货时间" prop="shippingTime">-->
<!--        <el-date-picker clearable-->
<!--          v-model="queryParams.shippingTime"-->
<!--          type="date"-->
<!--          value-format="yyyy-MM-dd"-->
<!--          placeholder="请选择发货时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="收货时间" prop="closedTime">-->
<!--        <el-date-picker clearable-->
<!--          v-model="queryParams.closedTime"-->
<!--          type="date"-->
<!--          value-format="yyyy-MM-dd"-->
<!--          placeholder="请选择收货时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['system:appOrder:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['system:appOrder:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['system:appOrder:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['system:appOrder:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="appOrderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="id" align="center" prop="id" />-->
<!--      <el-table-column label="用户id" align="center" prop="appUserId" />-->
<!--      <el-table-column label="商户id" align="center" prop="shopId" />-->
<!--      <el-table-column label="地址id" align="center" prop="addressId" />-->
      <el-table-column label="订单号" align="center" prop="orderNumber" />
<!--      <el-table-column label="流水号" align="center" prop="waterNumber" />-->
      <el-table-column label="订单状态" align="center" prop="status" >
        <template slot-scope="scope">
<!--          1-待支付2-部分支付3-代发货4-已完成5-已退款6-已取消-->
          <el-tag v-if="scope.row.status == 1" size="small">待支付</el-tag>
          <el-tag v-if="scope.row.status == 2" size="small">部分支付</el-tag>
          <el-tag v-if="scope.row.status == 3" size="small">代发货</el-tag>
          <el-tag v-if="scope.row.status == 4" size="small">已完成</el-tag>
          <el-tag v-if="scope.row.status == 5" size="small">已退款</el-tag>
          <el-tag v-if="scope.row.status == 6" size="small">已取消</el-tag>
        </template>
      </el-table-column>
<!--      <el-table-column label="产品快照" align="center" prop="addressSnapshot" />-->
      <el-table-column label="总金额" align="center" prop="totalPrice" />
<!--      <el-table-column label="优惠金额" align="center" prop="discountPrice" />-->
      <el-table-column label="应付金额" align="center" prop="copePrice" />
<!--      <el-table-column label="实付金额" align="center" prop="actualPrice" />-->
<!--      <el-table-column label="是否删除" align="center" prop="isDelete" />-->
<!--      <el-table-column label="备注" align="center" prop="remark" />-->
<!--      <el-table-column label="优惠id" align="center" prop="discountId" />-->
<!--      <el-table-column label="支付次数" align="center" prop="payNum" />-->
      <el-table-column label="商品数量" align="center" prop="skuNum" />
      <el-table-column label="商品标题" align="center" prop="productTitle" />
<!--      <el-table-column label="主图" align="center" prop="skuPic" />-->
      <el-table-column label="主图集合" align="center" prop="skuPicList" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.skuPicList"></image-preview>
        </template>
      </el-table-column>
<!--      <el-table-column label="用户上级id" align="center" prop="userDadId" />-->
<!--      <el-table-column label="1-现金 2-支付宝 3-微信 4-刷卡" align="center" prop="payType" />-->
      <el-table-column label="支付时间" align="center" prop="createTime" width="180">
<!--        <template slot-scope="scope">-->
<!--          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>-->
<!--        </template>-->
      </el-table-column>
<!--      <el-table-column label="已支付次数" align="center" prop="alreadyPayNum" />-->
      <el-table-column label="发货时间" align="center" prop="shippingTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.shippingTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="收货时间" align="center" prop="closedTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.closedTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="商家优惠金额" align="center" prop="shopDiscountPrice" />-->
<!--      <el-table-column label="商家承担优惠比例" align="center" prop="shopScale" />-->
<!--      <el-table-column label="商家承担的优惠比例金额" align="center" prop="shopScalePrice" />-->
<!--      <el-table-column label="用户id" align="center" prop="userId" />-->
<!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
<!--        <template slot-scope="scope">-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-hasPermi="['system:appOrder:edit']"-->
<!--          >修改</el-button>-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['system:appOrder:remove']"-->
<!--          >删除</el-button>-->
<!--        </template>-->
<!--      </el-table-column>-->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户id" prop="appUserId">
          <el-input v-model="form.appUserId" placeholder="请输入用户id" />
        </el-form-item>
        <el-form-item label="商户id" prop="shopId">
          <el-input v-model="form.shopId" placeholder="请输入商户id" />
        </el-form-item>
        <el-form-item label="地址id" prop="addressId">
          <el-input v-model="form.addressId" placeholder="请输入地址id" />
        </el-form-item>
        <el-form-item label="订单号" prop="orderNumber">
          <el-input v-model="form.orderNumber" placeholder="请输入订单号" />
        </el-form-item>
        <el-form-item label="流水号" prop="waterNumber">
          <el-input v-model="form.waterNumber" placeholder="请输入流水号" />
        </el-form-item>
        <el-form-item label="产品快照" prop="addressSnapshot">
          <el-input v-model="form.addressSnapshot" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="总金额" prop="totalPrice">
          <el-input v-model="form.totalPrice" placeholder="请输入总金额" />
        </el-form-item>
        <el-form-item label="优惠金额" prop="discountPrice">
          <el-input v-model="form.discountPrice" placeholder="请输入优惠金额" />
        </el-form-item>
        <el-form-item label="应付金额" prop="copePrice">
          <el-input v-model="form.copePrice" placeholder="请输入应付金额" />
        </el-form-item>
        <el-form-item label="实付金额" prop="actualPrice">
          <el-input v-model="form.actualPrice" placeholder="请输入实付金额" />
        </el-form-item>
        <el-form-item label="是否删除" prop="isDelete">
          <el-input v-model="form.isDelete" placeholder="请输入是否删除" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="优惠id" prop="discountId">
          <el-input v-model="form.discountId" placeholder="请输入优惠id" />
        </el-form-item>
        <el-form-item label="支付次数" prop="payNum">
          <el-input v-model="form.payNum" placeholder="请输入支付次数" />
        </el-form-item>
        <el-form-item label="商品数量" prop="skuNum">
          <el-input v-model="form.skuNum" placeholder="请输入商品数量" />
        </el-form-item>
        <el-form-item label="商品标题" prop="productTitle">
          <el-input v-model="form.productTitle" placeholder="请输入商品标题" />
        </el-form-item>
        <el-form-item label="主图" prop="skuPic">
          <el-input v-model="form.skuPic" placeholder="请输入主图" />
        </el-form-item>
        <el-form-item label="主图集合" prop="skuPicList">
          <el-input v-model="form.skuPicList" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="用户上级id" prop="userDadId">
          <el-input v-model="form.userDadId" placeholder="请输入用户上级id" />
        </el-form-item>
        <el-form-item label="支付时间" prop="payTime">
          <el-date-picker clearable
            v-model="form.payTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择支付时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="已支付次数" prop="alreadyPayNum">
          <el-input v-model="form.alreadyPayNum" placeholder="请输入已支付次数" />
        </el-form-item>
        <el-form-item label="发货时间" prop="shippingTime">
          <el-date-picker clearable
            v-model="form.shippingTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择发货时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="收货时间" prop="closedTime">
          <el-date-picker clearable
            v-model="form.closedTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择收货时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="商家优惠金额" prop="shopDiscountPrice">
          <el-input v-model="form.shopDiscountPrice" placeholder="请输入商家优惠金额" />
        </el-form-item>
        <el-form-item label="商家承担优惠比例" prop="shopScale">
          <el-input v-model="form.shopScale" placeholder="请输入商家承担优惠比例" />
        </el-form-item>
        <el-form-item label="商家承担的优惠比例金额" prop="shopScalePrice">
          <el-input v-model="form.shopScalePrice" placeholder="请输入商家承担的优惠比例金额" />
        </el-form-item>
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户id" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAppOrder, getAppOrder, delAppOrder, addAppOrder, updateAppOrder } from "@/api/system/appOrder";

export default {
  name: "AppOrder",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单表格数据
      appOrderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        appUserId: null,
        shopId: null,
        addressId: null,
        orderNumber: null,
        waterNumber: null,
        status: null,
        addressSnapshot: null,
        totalPrice: null,
        discountPrice: null,
        copePrice: null,
        actualPrice: null,
        isDelete: null,
        discountId: null,
        payNum: null,
        skuNum: null,
        productTitle: null,
        skuPic: null,
        skuPicList: null,
        userDadId: null,
        payType: null,
        payTime: null,
        alreadyPayNum: null,
        shippingTime: null,
        closedTime: null,
        shopDiscountPrice: null,
        shopScale: null,
        shopScalePrice: null,
        userId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询订单列表 */
    getList() {
      this.loading = true;
      listAppOrder(this.queryParams).then(response => {
        this.appOrderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        appUserId: null,
        shopId: null,
        addressId: null,
        orderNumber: null,
        waterNumber: null,
        status: null,
        addressSnapshot: null,
        totalPrice: null,
        discountPrice: null,
        copePrice: null,
        actualPrice: null,
        isDelete: null,
        createTime: null,
        updateTime: null,
        createBy: null,
        updateBy: null,
        remark: null,
        discountId: null,
        payNum: null,
        skuNum: null,
        productTitle: null,
        skuPic: null,
        skuPicList: null,
        userDadId: null,
        payType: null,
        payTime: null,
        alreadyPayNum: null,
        shippingTime: null,
        closedTime: null,
        shopDiscountPrice: null,
        shopScale: null,
        shopScalePrice: null,
        userId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAppOrder(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAppOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAppOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除订单编号为"' + ids + '"的数据项？').then(function() {
        return delAppOrder(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/appOrder/export', {
        ...this.queryParams
      }, `appOrder_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
